import { crtChannel } from "./crtChannel";



const { ccclass, property } = cc._decorator;

@ccclass
export default class crtBoxSurpriseView extends cc.Component {

    @property(cc.Sprite)
    progressBar: cc.Sprite = null;
    @property(cc.Node)
    clickBtn: cc.Node = null;
    @property(cc.Node)
    barImg: cc.Node = null;
    @property(cc.Node)
    box: cc.Node = null;

    showCustomBar: number = 0;
    showCustomAd: boolean = false;
    isWin: boolean = false;


    onLoad() {
        const me = this;
        this.clickBtn.on(cc.Node.EventType.TOUCH_END, () => {
            me.progressBar.fillRange += 0.08;
            me.barImg.x = me.progressBar.fillRange * 500 + (-250);
            if (me.progressBar.fillRange >= 1) {
                this.node.destroy();
                //@ts-ignore
                window.__prv.doAction(4, ['oppo']);
                //@ts-ignore
                window.__prv.doAction(4, ['vivo']);
                //@ts-ignore
                window.__prv.doAction(1, ['ks5']);
                //@ts-ignore
                window.__prv.doAction('二级界面弹出插屏', ['hw']);

            }
            else if (me.progressBar.fillRange >= this.showCustomBar && !this.showCustomAd) {
                this.showCustomAd = true;
                this.showCustom();
            }
        })
        this.refreshUI();
        // if(crtcha)
        if ((window as any).__prv.getChannel() == 'web') {
            let height = 0 - cc.winSize.height / 2;
            height += 60;
            this.clickBtn.y = height;
        } else if ((window as any).__prv && (window as any).__prv.getChannel() == 'oppo' || (window as any).__prv && (window as any).__prv.getChannel() == 'vivo') {
            let height = 0 - cc.winSize.height / 2;
            height += 60;
            this.clickBtn.y = height;
        }

    }

    showCustom() {
        console.error("狂点宝箱展示视频");
        //@ts-ignore
        window.__prv.doAction(3, ['ks']);
        //@ts-ignore
        window.__prv.doAction(2, ['oppo']);
        //@ts-ignore
        window.__prv.doAction(2, ['vivo']);
        //@ts-ignore
        window.__prv.showVideo(() => {

        })
    }

    refreshUI() {
        this.progressBar.fillRange = 0;
        this.barImg.x = -250;
        this.showCustomAd = false;
        this.showCustomBar = Math.random() * 0.3 + 0.35;
        cc.tween(this.box).repeatForever(cc.tween().to(0.3, { scale: 1.2 }).to(0.3, { scale: 1 })).start();
    }

    protected update(dt: number): void {
        if (this.progressBar.fillRange >= 1) {
            return;
        }
        if (this.progressBar.fillRange <= 0) {
            return;
        }
        this.progressBar.fillRange -= 0.0023;
        this.barImg.x = this.progressBar.fillRange * 500 + (-250);
    }


}
